<template>
	<view class="detail">
		<view class="title">
			{{news.title}}
		</view>
		<view class="time">
			<text>编辑：{{news.author}}</text>
			<text>发布日期：{{parseTime(news.posttime,'{y}-{m}-{d} {h}:{i}') }}</text>
		</view>
		<view class="content">
			<rich-text :nodes="news.content"></rich-text>
		</view>

	</view>

</template>

<script setup>
	import {
		parseTime
	} from '@/utils/tool.js'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		ref
	} from 'vue'
	const news = ref({})
	const option = ref(null)
	const historyNewsList = ref([])
	const getNewsDetail = () => {
		uni.request({
			url: 'https://ku.qingnian8.com/dataApi/news/detail.php',
			data: {
				id: option.value.id,
				cid: option.value.cid,
			},
			success(res) {
				res.data.content = res.data.content.replace(/<img/g, `<img style="max-width:100%"`)
				news.value = res.data
				uni.setNavigationBarTitle({
					title: res.data.title
				});
				saveHistory()
			}
		})
	}
	const saveHistory=()=>{
		historyNewsList.value=uni.getStorageSync("history") || []
		historyNewsList.value.map((current, index) => {
			if (current.id == news.value.id) {
				historyNewsList.value.splice(index, 1)
			}
		})
		historyNewsList.value.unshift({
			classid: news.value.classid,
			id: news.value.id,
			picurl: news.value.picurl,
			title: news.value.title,
			date: new Date(),
		})
		uni.setStorageSync('history', historyNewsList.value)
	}

	onLoad((val) => {
		option.value = val
		getNewsDetail()

	})
</script>

<style lang="scss" scoped>
	.detail {
		padding: 30rpx;

		.title {
			font-size: 50rpx;
		}

		.time {
			text {
				white-space: nowrap;
			}

			display: flex;
			justify-content: space-between;
			background-color:#F6F6F6;
			height: 60rpx;
			line-height: 60rpx;
			padding:0 20rpx;
			margin-top: 20rpx;
		}

		.content {
			margin-top: 20rpx;
		}

		img {
			max-width: 100%;
		}
	}
</style>